# =============================================================================
# APPENDIX I FIGURE 12A: DISTRIBUTION BY REGIME TYPE
# Distribution of estimates for closed vs electoral autocracies (non-independent sample)
# =============================================================================

# --- PACKAGES ----------------------------------------------------------------
library(readr)
library(haven)
library(dplyr)
library(ggplot2)
library(tidyr)

# --- DATA LOADING ------------------------------------------------------------
estimates <- read_csv("estimates_colony_2024.csv")

# --- VISUALIZATION -----------------------------------------------------------
p_density <- ggplot(estimates, aes(x = dyn.estimates, y = after_stat(density), 
                                   fill = factor(v2x_regime))) +
  geom_density(alpha = 0.5) +
  scale_fill_manual(values = c("0" = "grey50", "1" = "grey80"),
                    name = "", 
                    labels = c("Closed Autocracies", "Electoral Autocracies")) +
  scale_x_continuous(limits = c(-8, 4), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 0.8), expand = c(0, 0)) +
  labs(x = expression(Latent~estimate~theta), y = "Density", title = NULL) +
  theme_classic() +
  theme(legend.position = "bottom")

# --- OUTPUT ------------------------------------------------------------------
ggsave("density_closed_electoral.pdf", p_density)
